import axios from 'axios';

const API_URL = 'http://localhost:8087/api';

const apiClient = axios.create({
    baseURL: API_URL,
    headers: {
        'Content-Type': 'application/json',
        'Accept': '*/*'
    }
});

// 请求拦截器 - 添加认证令牌
apiClient.interceptors.request.use(
    config => {
        const token = localStorage.getItem('auth-token');
        if (token) {
            config.headers['Authorization'] = `Bearer ${token}`;
        }
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

// 响应拦截器 - 处理错误
apiClient.interceptors.response.use(
    response => {
        return response;
    },
    error => {
        if (error.response && error.response.status === 401) {
            // 未授权，清除令牌并重定向到登录页面
            localStorage.removeItem('auth-token');
            localStorage.removeItem('user-info');
            window.location.href = '/auth/login';
        }
        return Promise.reject(error);
    }
);

export default {
    // 用户登录
    login(email, password) {
        return apiClient.post('/admin/user/login', {
            email: email,
            password: password
        });
    },
    
    // 获取用户信息
    getUserInfo() {
        return apiClient.get('/admin/user/info');
    }
}; 